package com.stylefeng.guns.modular.system.controller;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.stylefeng.guns.modular.system.service.impl.DictServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.stylefeng.guns.common.annotion.BussinessLog;
import com.stylefeng.guns.common.annotion.Permission;
import com.stylefeng.guns.common.constant.Const;
import com.stylefeng.guns.common.constant.dictmap.DictMap;
import com.stylefeng.guns.common.constant.factory.ConstantFactory;
import com.stylefeng.guns.common.exception.BizExceptionEnum;
import com.stylefeng.guns.common.persistence.dao.DictMapper;
import com.stylefeng.guns.common.persistence.model.Dict;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.dao.DictDao;
import com.stylefeng.guns.modular.system.service.IDictService;
import com.stylefeng.guns.modular.system.warpper.DictWarpper;

/**
 * 字典控制器
 *
 * @author fengshuonan
 * @Date 2017年4月26日 12:55:31
 */
@Controller
@RequestMapping("/dict")
public class DictController extends BaseController {

    private String PREFIX = "/system/dict/";

    @Resource
    DictDao dictDao;

    @Resource
    DictMapper dictMapper;

    @Resource
    DictServiceImpl dictService;

    /**
     * 跳转到字典管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "dict.html";
    }

    /**
     * 跳转到添加字典
     */
    @RequestMapping("/dict_add")
    public String deptAdd() {
        return PREFIX + "dict_add.html";
    }

    /**
     * 跳转到修改字典
     */
    @RequestMapping("/dict_edit/{dictId}")
    public String dictUpdate(@PathVariable Integer dictId, Model model) {
        Dict dict = dictMapper.selectById(dictId);
        model.addAttribute("dict", dict);
        return PREFIX + "dict_edit.html";
    }


    /**
     * 跳转到修改字典
     */
    //@Permission(Const.ADMIN_NAME)
    @RequestMapping("/dictById/{dictId}")
    @ResponseBody
    public Object dictById(@PathVariable Integer dictId) {
        Dict dict = dictMapper.selectById(dictId);
        LogObjectHolder.me().set(dict);
        return dict;
    }

    /**
     * 新增字典
     */
    @BussinessLog(value = "添加字典记录", key = "dictNum", dict = DictMap.class)
    @RequestMapping(value = "/add")
    //@Permission(Const.ADMIN_NAME)
    @ResponseBody
    public Object add(@Param("pidname") String pidname, @Param("name") String name, @Param("tips") String tips, @Param("num") Integer num) {
        if (name == "" || name == null || tips == null || tips == "") {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        Integer pidnum = 0;
        if (pidname == "" || pidname == null) {
            pidnum = 0;
        } else {
            List<Dict> list = dictDao.selectPid(pidname);
            for (Dict dict : list) {
                pidnum = dict.getId();
            }
        }
        Dict dict = new Dict(num, pidnum, name, tips, 1);
        dictMapper.insert(dict);
        return SUCCESS_TIP;
    }

    /**
     * 获取所有字典列表
     */
    @RequestMapping(value = "/list")
    //@Permission(Const.ADMIN_NAME)
    @ResponseBody
    public Object list(@Param("condition") String condition, @Param("pidname") String pidname) {
        Integer pid = -1;
        if (pidname != null && pidname != "") {
            Dict parentCondition = new Dict();
            parentCondition.setName(pidname);
            Dict parent = dictMapper.selectOne(parentCondition);
            if (parent != null) {
                pid = parent.getId();
            }
        }
        List<Map<String, Object>> list = this.dictDao.list(pid, condition);
        return super.warpObject(new DictWarpper(list));
    }

    /**
     * 字典详情
     */
    @RequestMapping(value = "/detail/{dictId}")
    //@Permission(Const.ADMIN_NAME)
    @ResponseBody
    public Object detail(@PathVariable("dictId") Integer dictId) {
        return dictMapper.selectById(dictId);
    }

    /**
     * 修改字典
     */
    @BussinessLog(value = "修改字典", key = "dictId", dict = DictMap.class)
    @RequestMapping(value = "/update")
    //@Permission(Const.ADMIN_NAME)
    @ResponseBody
    public Object update(Dict dict) {
        if (ToolUtil.isOneEmpty(dict)) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        if (ToolUtil.isEmpty(dict.getId())) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        dictService.updateById(dict);
        return super.SUCCESS_TIP;
    }

    /**
     * 删除字典记录
     */
    @BussinessLog(value = "删除字典记录", key = "dictId", dict = DictMap.class)
    @RequestMapping(value = "/delete")
    //@Permission(Const.ADMIN_NAME)
    @ResponseBody
    public Object delete(@RequestParam Integer dictId) {

        //缓存被删除的名称
        LogObjectHolder.me().set(ConstantFactory.me().getDictName(dictId));

        this.dictService.delteDict(dictId);


        return SUCCESS_TIP;
    }

    /**
     * 修改状态为启用
     */
    @BussinessLog(value = "修改字典状态为启用", key = "dictId", dict = DictMap.class)
    @RequestMapping(value = "/statusupdate")
    //@Permission(Const.ADMIN_NAME)
    @ResponseBody
    public Object statusupdate(@RequestParam Integer dictId) {
        //查询
        Dict dict = dictMapper.selectById(dictId);
        if (dict.getStatus() != 1) {
            dict.setStatus(1);
            dictMapper.updateAllColumnById(dict);
            return SUCCESS_TIP;
        }

        return SUCCESS_TIP;

    }

    /**
     * 修改状态为禁用
     */
    @BussinessLog(value = "修改字典状态为禁用", key = "dictId", dict = DictMap.class)
    @RequestMapping(value = "/statusupdateno")
    //@Permission(Const.ADMIN_NAME)
    @ResponseBody
    public Object statusupdateno(@RequestParam Integer dictId) {
        //查询
        Dict dict = dictMapper.selectById(dictId);
        //修改
        if (dict.getStatus() != 0) {
            dict.setStatus(0);
            dictMapper.updateAllColumnById(dict);
            return SUCCESS_TIP;
        }
        return SUCCESS_TIP;
    }

    /**
     * 获取字典父类树节点列表
     *
     * @return
     */
    @RequestMapping(value = "/dicttree")
    @ResponseBody
    public Object Dicttree() {
        return dictService.Dicttree();
    }

}
